﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;

namespace MissionImpossible
{
  public partial class PeoplesEditor : Form
  {
    private AddPeopleDialog peopleAddingDialog;
    private DataView peoplesDataView;
    public PeoplesEditor()
    {
      InitializeComponent();


    }

    private void closeButton_Click(object sender, EventArgs e)
    {
      Hide();
    }

    private void PeoplesEditor_Load(object sender, EventArgs e)
    {
      peopleListTableAdapter.Fill(filmDatabaseDataSet.PeopleList);
    }

    private void UpdateTables()
    {
      peopleListTableAdapter.Fill(filmDatabaseDataSet.PeopleList);
    }

    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
    }

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {
      ShowAddDialog();
    }

    private void ShowAddDialog()
    {
      if(peopleAddingDialog != null)
      {
        peopleAddingDialog.BringToFront();
      }
      else
      {
        peopleAddingDialog = new AddPeopleDialog();
        peopleAddingDialog.Closed += addPeopleDialog_Closed;
        peopleAddingDialog.ShowDialog();
      }
    }

    void addPeopleDialog_Closed(object sender, EventArgs e)
    {
      SaveEnteredData(sender as AddPeopleDialog);
    }

    private void SaveEnteredData(AddPeopleDialog addPeopleDialog)
    {
      peopleAddingDialog = null;
      AddPeopleDialog.PeopleData data = addPeopleDialog.EnteredData;
      if(data == null)
      {
        return;
      }
      peopleListTableAdapter.InsertPeople(data.Name, data.Lastname, data.Birthdate);
      UpdateTables();
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
      int rowIndex = e.RowIndex;
      int columnIndex = e.ColumnIndex;
      if(rowIndex == -1 || columnIndex == -1)
      {
        return;
      }
      switch(columnIndex)
      {
        case 1:
        case 2:
        case 3:
          UpdatePeople(rowIndex);
          return;
        case 4:
          UpdatePeopleActorMode(rowIndex);
          return;
        case 5:
          UpdatePeopleActor(rowIndex);
          return;
        case 6:
          UpdatePeopleDirectorMode(rowIndex);
          return;
        case 7:
          UpdatePeopleDirector(rowIndex);
          return;
      }
    }

    private void UpdatePeopleDirector(int rowIndex)
    {
    }

    private void UpdatePeopleDirectorMode(int rowIndex)
    {
    }

    private void UpdatePeopleActor(int rowIndex)
    {
    }

    private void UpdatePeopleActorMode(int rowIndex)
    {
    }

    private void UpdatePeople(int rowIndex)
    {
      long id = (long)GetData(rowIndex, 0);
      string name = (string)GetData(rowIndex, 1);
      string surname = (string)GetData(rowIndex, 2);
      DateTime datebirth = (DateTime)GetData(rowIndex, 3);
      peopleListTableAdapter.UpdatePeople(name, surname, datebirth, id);
    }

    private object GetData(int rowIndex, int cellIndex)
    {
      return dataGridView1.Rows[rowIndex].Cells[cellIndex].Value;
    }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
    }

    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
      
    }
  }
}
